home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 May / SGI IRIX 6.2 Applications 1996 May.iso / dist / impr_dev.idb / usr / impressario / src / examples / libspool / state.c.z / state.c
C/C++ Source or Header  |  1996-05-06  |  6KB  |  214 lines

  1. /**************************************************************************
  2.  *                                      *
  3.  *           Copyright (c)    1991 Silicon Graphics, Inc.          *
  4.  *            All Rights Reserved                    *
  5.  *                                      *
  6.  *       THIS    IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI          *
  7.  *                                      *
  8.  * The copyright notice above does not evidence any actual of intended      *
  9.  * publication of such source code, and is an unpublished work by Silicon *
  10.  * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is *
  11.  * the property of Silicon Graphics, Inc. Any use, duplication or      *
  12.  * disclosure not specifically authorized by Silicon Graphics is strictly *
  13.  * prohibited.                                  *
  14.  *                                      *
  15.  * RESTRICTED RIGHTS LEGEND:                          *
  16.  *                                      *
  17.  * Use, duplication or disclosure by the Government is subject to      *
  18.  * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in      *
  19.  * Technical Data and Computer Software clause at DFARS 52.227-7013,      *
  20.  * and/or in similar or successor clauses in the FAR, DOD or NASA FAR      *
  21.  * Supplement. Unpublished - rights reserved under the Copyright Laws of  *
  22.  * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.      *
  23.  * Shoreline Blvd., Mountain View, CA 94039-7311              *
  24.  **************************************************************************
  25.  *
  26.  * File: state.c
  27.  *
  28.  * Description: Sets or gets the state of the spooling system for the
  29.  *    specified printer.
  30.  *
  31.  **************************************************************************/
  32.  
  33.  
  34. #ident "$Revision: 1.1 $"
  35.  
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #ifdef sgi
  41. #include <getopt.h>
  42. #endif
  43. #include "spool.h"
  44.  
  45.  
  46. #define GET_OP        1
  47. #define SET_OP        0
  48.  
  49.  
  50. char *printer;
  51. int spooler = SL_SPOOLER_NONE;
  52.  
  53.  
  54. extern char *optarg;
  55. extern int optind;
  56.  
  57.  
  58. void usage(char*);
  59.  
  60.  
  61. main(int argc, char *argv[])
  62. {
  63.     int ch, operation;
  64.     int errflag = 0;
  65.     int function, state, old_state, new_state;
  66.     char *funcname;
  67.  
  68.     /*
  69.      * Process command line args
  70.      */
  71.     while ((ch = getopt(argc, argv, "d:s:")) != -1) {
  72.     switch(ch) {
  73.         case 'd':        /* Destination printer */
  74.         printer = optarg;
  75.         break;
  76.         case 's':        /* Spooler to use */
  77.         if (!strcmp(optarg, "bsd"))
  78.             spooler = SL_SPOOLER_BSD;
  79.         else if (!strcmp(optarg, "sysv"))
  80.             spooler = SL_SPOOLER_SYSV;
  81.         else
  82.             errflag++;
  83.         break;
  84.         case '?':
  85.         default:
  86.         errflag++;
  87.         break;
  88.     }
  89.     }
  90.  
  91.     /*
  92.      * Get the function - printing state or queueing state
  93.      */
  94.     if (argc == optind)
  95.     errflag++;
  96.     else {
  97.     funcname = argv[optind];
  98.     if (!strcmp(argv[optind], "printing"))
  99.         function = SL_PRINTING;
  100.     else if (!strcmp(argv[optind], "queueing"))
  101.         function = SL_QUEUEING;
  102.     else
  103.         errflag++;
  104.     }
  105.  
  106.     /*
  107.      * Get the state, if any. If a state is specified this is
  108.      * a set operation. If no state is specified, this is a 
  109.      * get operation.
  110.      */
  111.     if (!errflag) {
  112.     optind++;
  113.     if (argc == optind) {
  114.         operation = GET_OP;
  115.     } else {
  116.         operation = SET_OP;
  117.         if (!strcmp(argv[optind], "enable"))
  118.             state = SL_ENABLED;
  119.         else if (!strcmp(argv[optind], "disable"))
  120.             state = SL_DISABLED;
  121.         else
  122.             errflag++;
  123.     }
  124.     }
  125.  
  126.     /*
  127.      * If error print usage and exit
  128.      */
  129.     if (errflag) {
  130.     usage(argv[0]);
  131.     exit(1);
  132.     }
  133.  
  134.     /*
  135.      * If a spooling system has been specified set it
  136.      */
  137.     if (spooler != SL_SPOOLER_NONE) {
  138.         if (SLSetSpooler(spooler) < 0) {
  139.             SLPerror(argv[0]);
  140.             exit(1);
  141.         }
  142.     }
  143.  
  144.     if (operation == SET_OP) {
  145.         /*
  146.          * Get the old state
  147.          */
  148.         if (SLGetSpoolerState(printer, function, &old_state) < 0) {
  149.             int i, status, nout;
  150.         char **out_buf;
  151.         SLPerror(argv[0]);
  152.         if ((status = SLGetSpoolerError(&out_buf, &nout)) != 0) {
  153.             (void)fprintf(stderr, "Spooler exit status: %d\n", status);
  154.             (void)fprintf(stderr, "Spooler error message(s):\n");
  155.             for (i = 0; i < nout; i++)
  156.                 (void)fprintf(stderr, "\t%s\n", out_buf[i]);
  157.         }
  158.         exit(1);
  159.         }
  160.  
  161.         /*
  162.          * Set the new state
  163.          */
  164.         if (SLSetSpoolerState(printer, function, state) < 0) {
  165.         int i, status, nout;
  166.         char **out_buf;
  167.         SLPerror(argv[0]);
  168.         if ((status = SLGetSpoolerError(&out_buf, &nout)) != 0) {
  169.             (void)fprintf(stderr, "Spooler exit status: %d\n", status);
  170.             (void)fprintf(stderr, "Spooler error message(s):\n");
  171.             for (i = 0; i < nout; i++)
  172.                 (void)fprintf(stderr, "\t%s\n", out_buf[i]);
  173.         }
  174.         exit(1);
  175.         }
  176.     }
  177.  
  178.     /*
  179.      * Get the new state
  180.      */
  181.     if (SLGetSpoolerState(printer, function, &new_state) < 0) {
  182.         int i, status, nout;
  183.     char **out_buf;
  184.     SLPerror(argv[0]);
  185.     if ((status = SLGetSpoolerError(&out_buf, &nout)) != 0) {
  186.         (void)fprintf(stderr, "Spooler exit status: %d\n", status);
  187.         (void)fprintf(stderr, "Spooler error message(s):\n");
  188.         for (i = 0; i < nout; i++)
  189.             (void)fprintf(stderr, "\t%s\n", out_buf[i]);
  190.     }
  191.     exit(1);
  192.     }
  193.  
  194.     /*
  195.      * Print the results
  196.      */
  197.     (void)printf("Printer: %s\n", (printer) ? printer: "default");
  198.     if (operation == SET_OP) {
  199.         (void)printf("Original state of %s: %s\n", funcname,
  200.             (old_state == SL_ENABLED) ? "enabled": "disabled");
  201.     }
  202.     (void)printf("Current state of %s: %s\n", funcname,
  203.         (new_state == SL_ENABLED) ? "enabled": "disabled");
  204.  
  205.     return 0;
  206. }
  207.  
  208.  
  209. void usage(char *progname)
  210. {
  211.     (void)fprintf(stderr, "%s [-d printer] [-s bsd | sysv] ", progname);
  212.     (void)fprintf(stderr, "printing | queueing [enable | disable]\n");
  213. }
  214.